package com.ex.orage2app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;

import androidx.annotation.Nullable;

public class OrangeDatabase extends SQLiteOpenHelper {
    public OrangeDatabase(@Nullable Context context) {
        super(context, "orange", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="create table orange_user(id integer primary key autoincrement," +
                "username varchar(50),password varchar(50),sex varchar(10),city carchar(50))";
        sqLiteDatabase.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    //插入数据
    public void insertUser(SQLiteDatabase sqLiteDatabase,String username,String password,
                           String sex,String city){
        ContentValues contentValues=new ContentValues();
        contentValues.put("username",username);
        contentValues.put("password",password);
        contentValues.put("sex",sex);
        contentValues.put("city",city);
        sqLiteDatabase.insert("orange_user",null,contentValues);
        sqLiteDatabase.close();
    }
    //查询数据
    public Bundle queryUserInfo(SQLiteDatabase sqLiteDatabase, Bundle bundle){
        String username=bundle.getString("username");
        Cursor cursor=sqLiteDatabase.rawQuery("select * from " +
                "orange_user where username=?",new String[]{username});
        if(cursor!=null){
            while (cursor.moveToNext()){
                bundle.putString("sex",cursor.getString(3));
                bundle.putString("city",cursor.getString(4));

            }
        }
        cursor.close();
        sqLiteDatabase.close();
        return bundle;
    }

}
